﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Office2010.Excel;
using ThridGroup.ERP.DTO.ProductionSystem;
using ThridGroup.ERP.HumanResources;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace ThridGroup.ERP.ProductionSystem
{
    /// <summary>
    /// 排班计划仓储
    /// </summary>
    public class CrewSchedulingServices : ApplicationService, ICrewSchedulingServices
    {
        /// <summary>
        /// 排版计划DTO
        /// </summary>
        private readonly IRepository<CrewScheduling, int> _repository;
        /// <summary>
        /// 班次DTO
        /// </summary>
        private readonly IRepository<SailingsModel, int> _salings;
        /// <summary>
        ///构造函数
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="salings"></param>
        public CrewSchedulingServices(IRepository<CrewScheduling, int> repository, IRepository<SailingsModel, int> salings)
        {
            _repository = repository;
            _salings = salings;
        }
        /// <summary>
        /// 添加排班计划
        /// </summary>
        /// <param name="crewScheduling"></param>
        /// <returns></returns>
        public async Task<int> AddCrewScheduling(CrewSchedulingDTO crewScheduling)
        {
            var entity = ObjectMapper.Map<CrewSchedulingDTO, CrewScheduling>(crewScheduling);
            var result = await _repository.InsertAsync(entity);
            return result == null ? 0 : 1;
        }
        /// <summary>
        /// 删除排班计划
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task DelCrewScheduling(List<int> ids)
        {
            await _repository.DeleteDirectAsync(m => ids.Contains(m.Id));
        }

        /// <summary>
        /// 获取排班计划信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<CrewSchedulingDTO> GetCrewScheduling(int id)
        {
            var Crew = await _repository.FindAsync(m => m.Id == id);
            var result = ObjectMapper.Map<CrewScheduling, CrewSchedulingDTO>(Crew);
            return result;
        }

        /// <summary>
        /// 显示排班计划列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="planName"></param>
        /// <returns></returns>
        public async Task<List<CrewSchedulingDTO>> ShowCrewScheduling(int pageIndex = 1, int pageSize = 10, string? planName = "")
        {
            var list = await _repository.GetListAsync();
            list = list.WhereIf(!string.IsNullOrEmpty(planName), m => m.PlanName.Contains(planName)).ToList();
            var result = ObjectMapper.Map<List<CrewScheduling>, List<CrewSchedulingDTO>>(list);
            return result;
        }

        /// <summary>
        /// 编辑排班计划列表
        /// </summary>
        /// <param name="crewScheduling"></param>
        /// <returns></returns>
        public async Task<int> UpdateCrewScheduling(CrewSchedulingDTO crewScheduling)
        {
            var entity = ObjectMapper.Map<CrewSchedulingDTO, CrewScheduling>(crewScheduling);
            var result = await _repository.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }

        //==================================班次=======================================

        /// <summary>
        /// 添加班次
        /// </summary>
        /// <param name="sailings"></param>
        /// <returns></returns>
        public async Task<int> AddSailings(SailingsModelDTO sailings)
        {
            var entity = ObjectMapper.Map<SailingsModelDTO, SailingsModel>(sailings);
            var result = await _salings.InsertAsync(entity);
            return result == null ? 0 : 1;
        }
        /// <summary>
        /// 显示班次列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<SailingsModelDTO>> ShowSailings(int? id=0)
        {
            var list = await _salings.GetListAsync(m => m.CrewId == id);
            var result = ObjectMapper.Map<List<SailingsModel>, List<SailingsModelDTO>>(list.OrderBy(m=>m.Id).ToList());
            return result;
        }
        /// <summary>
        /// 删除班次
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task DeleteSailings(List<int> ids)
        {
            await _salings.DeleteDirectAsync(m => ids.Contains(m.Id));
        }
        /// <summary>
        /// 获取班次
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SailingsModelDTO> GetSailings(int id)
        {
            var sailing = await _salings.FindAsync(m => m.Id == id);
            var result = ObjectMapper.Map<SailingsModel, SailingsModelDTO>(sailing);
            return result;
        }
        /// <summary>
        /// 编辑班次
        /// </summary>
        /// <param name="sailings"></param>
        /// <returns></returns>
        public async Task<int> UpdateSailings(SailingsModelDTO sailings) 
        {
            var entity = ObjectMapper.Map<SailingsModelDTO, SailingsModel>(sailings);
            var result = await _salings.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }
    }
}
